// pages/ScenicArea/ScenicAreaGoodsList/index.js
const utils = require('../utils.js')
const common = require('../../../public.js')
const config = require('../../../config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isFyApp: config.isFyApp,
    active: 1,
    storeId: 0,
    moduleType: 1,
    cateIndex: -1,
    defaultItemList: [],
    storeCatList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.storeId = options.id ? options.id : config.id
    this.data.moduleType = options.moduleType ? options.moduleType : config.type
  },

  onChangeTabbar(e) { //切换底部导航
    const index = e.detail.index
    const url = e.detail.url
    if (index != this.data.active) {
      utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
    }
  },

  goodsDetail(e) {
    common.gotoGoodsPage(e.currentTarget.dataset.item)
  },

  changeCategory(idx, index) {
    this.setData({
      isLoading: true
    })
    const that = this
    const tempJson = {
      cid: idx,
      storeId: that.data.storeId,
      token: that.data.token,
      userId: that.data.userInfo.id
    }
    common.post('/store/mainPageInfo/cid', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          defaultItemList: res.data.data,
        })
      } else {
        that.setData({
          defaultItemList: []
        })
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoading: false
      })
    })
  },
  categoryClick(e) {
    const idx = e.currentTarget.dataset.id
    const index = e.currentTarget.dataset.index;
    if (this.data.cateIndex === index) {
      return
    }
    const that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token,
          openid: data.openid,
          cateIndex: index
        })
        that.changeCategory(idx)
      }
    }, true)
  },
})